﻿<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>sewiki安装程序 - 我是正经的安装君</title>
	</head>
	<body>
		<form method="post">
			<table style="margin:auto">
				<thead>
					<tr style="background:#f0f;color:white">
						<td colspan="2" align="center">sewiki安装程序</td>
					</tr>
				</thead>
				<tbody style="background:#efe">
				<tr>
					<td>数据库服务器</td>
					<td><input name="dbserver" type="text" onMouseOver="this.focus()" onMouseOut="if(this.value=='')this.value='localhost'"></td>
				</tr>
				<tr>
					<td>数据库名</td>
					<td><input name="dbname" type="text"></td>
				</tr>
				<tr>
					<td>数据库用户名</td>
					<td><input name="dbuser" type="text"></td>
				</tr>
				<tr>
					<td>数据库密码</td>
					<td><input name="dbpasswd" type="text"></td>
				</tr>
				<tr>
					<td>来点样例</td>
					<td>
						好<input name="sample" type="radio" checked="checked" value="1">
						不了<input name="sample" type="radio" value="0">
					</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input name="submit" type="submit" value="提交" />
					</td>
				</tr>
				<tr>
					<td colspan="2"><pre></pre></td>
				</tr>
				</tbody>
			</table>
		</form>
	</body>
</html>
<?php
if(isset($_POST['submit'])) {
	$db_server = $_POST['dbserver'];
	$db_name   = $_POST['dbname'];
	$db_user   = $_POST['dbuser'];
	$db_passwd = $_POST['dbpasswd'];
	
	if($db_name == '' || $db_user == '') {
		exit();
	}
	if(mysql_connect($db_server, $db_user, $db_passwd) == false) {
		echo '数据库信息不对';
		exit();}
	
	$settings  = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n";
	$settings .= "\$active_group = 'default';" . "\n";
	$settings .= "\$active_record = TRUE;" . "\n";

	$settings .= "\$db['default']['hostname'] = 'localhost';" . "\n";
	$settings .= "\$db['default']['username'] = '" . $db_user . "';\n";
	$settings .= "\$db['default']['password'] = '" . $db_passwd . "';\n";
	$settings .= "\$db['default']['database'] = '" . $db_name . "';\n";
	
	$settings .= "\$db['default']['dbdriver'] = 'mysql';\n\$db['default']['dbprefix'] = '';\n\$db['default']['pconnect'] = TRUE;\n\$db['default']['db_debug'] = TRUE;\n\$db['default']['cache_on'] = FALSE;\n\$db['default']['cachedir'] = '';\n\$db['default']['char_set'] = 'utf8';\n\$db['default']['dbcollat'] = 'utf8_general_ci';\n\$db['default']['swap_pre'] = '';\n\$db['default']['autoinit'] = TRUE;\n\$db['default']['stricton'] = FALSE;";
	echo "<pre>" . $settings . "</pre>";
	
	@touch('application/config/database.php');
	$file = @fopen('application/config/database.php', 'w');
	if($file) {
		fwrite($file, $settings);
		fclose($file);
	}
	else {
		echo 'fuck';
		die();
	}

	$query = 'CREATE DATABASE IF NOT EXISTS ' . $db_name . '';
	echo $query;
	mysql_query($query);
	
	if(mysql_select_db($db_name) == 0) {
		echo 'Connection Failed';
		exit();
	}

	if(isset($_POST['sample']) && $_POST['sample'] == 1) {
		$cmd = "mysql -u" . $db_user;
		if($db_passwd == '')
			$cmd .= " " . $db_name . " < sewiki.sql";
		else
			$cmd .= " -p" . $db_passwd . " " . $db_name . " < sewiki.sql";
		echo $cmd;
//		exit();
		exec($cmd);
	}

	$url = $_SERVER['PHP_SELF'];
	$url = str_replace('setup.php', 'index.php/main', $url);
	header('Location: ' . $url);
}
?>
